<?php

/**
 * categories actions.
 *
 * @package    label
 * @subpackage categories
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class frontend_labelsActions extends sfActions {
	public function executeIndex(sfWebRequest $request) {
		
		$this->form = new FrontendLabelsForm ();
		
		$this->labels = Doctrine_Core::getTable ( 'FrontendLabels' )->getByLanguage ( sfConfig::get ( 'app_english' ) );
		
		$directory_id = $this->getUser()->getAttribute("directory_id");
		
		$directory = MasterDirectoryTable::getInstance()->find($directory_id);
		
		$this->language_id = $directory->getLanguageId();
		
		/*$layout = $this->getUser ()->getAttribute ( 'language' )->getId () == sfConfig::get ( 'app_english' ) ? 'layout_dictionary' : 'layout';
		$this->setLayout ( $layout );*/
	
		//$this->languages = $this->getUser()->getAttribute("user_languages");
	//$this->languages = array();
	}
	
	public function executeShow(sfWebRequest $request) {
		$this->label = Doctrine_Core::getTable ( 'FrontendLabels' )->find ( array ($request->getParameter ( 'id' ) ) );
		$this->forward404Unless ( $this->label );
		
		$this->form = new FrontendLabelsForm ( $this->label );
		
		$all_labels = Doctrine_Core::getTable ( 'FrontendLabels' )->getByLanguage ( sfConfig::get ( 'app_english' ) );
		
		$languages = $this->getUser ()->getAttribute ( "user_languages" );
		
		return $this->renderPartial ( 'label', array ('form' => $this->form, 'labels' => $all_labels, 'languages' => $languages ) );
	
	}
	
	public function executeNew(sfWebRequest $request) {
		$this->form = new FrontendLabelsForm ();
	}
	
	public function executeCreate(sfWebRequest $request) {
		
		$this->forward404Unless ( $request->isMethod ( sfRequest::POST ) );
		
		$form = new FrontendLabelsForm ();
		
		$this->processForm ( $request, $form );
		
		$this->labels = Doctrine_Core::getTable ( 'FrontendLabels' )->getByLanguage ( sfConfig::get ( 'app_english' ) );
		
		$this->form = new FrontendLabelsForm ();
		
		$language_id = $request->getParameter("language_id");
		
		return $this->renderPartial ( 'label_container', array ('labels' => $this->labels, 'form' => $this->form, 'language_id' => $language_id ) );
	}
	
	public function executeEdit(sfWebRequest $request) {
		$this->forward404Unless ( $label = Doctrine_Core::getTable ( 'FrontendLabels' )->find ( array ($request->getParameter ( 'id' ) ) ), sprintf ( 'Object label does not exist (%s).', $request->getParameter ( 'id' ) ) );
		$this->form = new FrontendLabelsForm ( $label );
	
	}
	
	public function executeUpdate(sfWebRequest $request) {
		$this->forward404Unless ( $request->isMethod ( sfRequest::POST ) || $request->isMethod ( sfRequest::PUT ) );
		$this->forward404Unless ( $label = Doctrine_Core::getTable ( 'FrontendLabels' )->find ( array ($request->getParameter ( 'id' ) ) ), sprintf ( 'Object label does not exist (%s).', $request->getParameter ( 'id' ) ) );
		$form = new FrontendLabelsForm ( $label );
		
		$this->processForm ( $request, $form );
		
		$labels = array ();
		
		array_push ( $labels, Doctrine_Core::getTable ( 'FrontendLabels' )->find ( array ($this->getUser ()->getAttribute ( "label_id" ) ) ) );
		
		$all_labels = $this->labels = Doctrine_Core::getTable ( 'FrontendLabels' )->getByLanguage ( sfConfig::get ( 'app_english' ) );
		//$this->labels = $this->getUser()->getAttribute('rol_id') == sfConfig::get('app_admin') ? $all_labels : $labels;
		

		$this->form = new FrontendLabelsForm ();
		
		$languages = $this->getUser ()->getAttribute ( "user_languages" );
		
		return $this->renderPartial ( 'label_container', array ('labels' => $all_labels, 'form' => $this->form, 'languages' => $languages ) );
	
	}
	
	public function executeDelete(sfWebRequest $request) {
		//$request->checkCSRFProtection();
		

		$this->forward404Unless ( $label = Doctrine_Core::getTable ( 'FrontendLabels' )->find ( array ($request->getParameter ( 'id' ) ) ), sprintf ( 'Object label does not exist (%s).', $request->getParameter ( 'id' ) ) );
		$label->delete ();
		
		$this->getUser ()->setFlash ( 'save', 'deleted', false );
		
		$this->labels = Doctrine_Core::getTable ( 'FrontendLabels' )->getByLanguage ( sfConfig::get ( 'app_english' ) );
		
		$this->form = new FrontendLabelsForm ();
		
		return $this->renderPartial ( 'label_container', array ('labels' => $this->labels, 'form' => $this->form ) );
	}
	
	public function executeSaveTranslation(sfWebRequest $request) {
		$language_id = $request->getParameter ( "language_id" );
		$labels = FrontendLabelsTable::getInstance ()->getByLanguage ( sfConfig::get ( 'app_english' ) );
		foreach ( $labels as $label ) {
			$translation_str = $request->getParameter ( $label->getId () . '_' . $language_id );
			$existing_translation = FrontendLabelsTable::getInstance ()->getTranslation ( $label->getId (), $language_id );
			if (trim ( $translation_str ) != "") {
				$translation = $existing_translation != null ? $existing_translation : new FrontendLabels ();
				$translation->setName ( utf8_encode ( $translation_str ) );
				$translation->setLabelId ( $label->getId () );
				$translation->setLanguageId ( $language_id );
				$translation->save ();
			} else if ($existing_translation != null) {
				$existing_translation->delete ();
			}
		}
		
		$this->form = new FrontendLabelsForm ();
		
		return $this->redirect("frontend_labels/index");
	}
	
	protected function processForm(sfWebRequest $request, sfForm $form) {
		
		$form->bind ( $request->getParameter ( $form->getName () ), $request->getFiles ( $form->getName () ) );
		/* if ($form->isValid())
		 {*/
		$label = $form->save ();
		
		$this->getUser ()->setFlash ( 'save', 'saved', false );
	
		//$this->redirect('labels/edit?id='.$label->getId());
	// }
	}

}
